﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace syc.Fc
{
    public partial class JinDu : System.Web.UI.Page
    {
        private string sql = "select [生产进度].[ID]"
            + ",[生产单位].[名称] as [分厂]"
            + ",[生产进度].[生产线]"
            + ",[生产进度].[日期]"
            + ",dbo.班组([生产进度].[工段]) as 工段"
            + ",[产品].[品名]+'('+[产品].[钢级]+')'+[产品].[规格] as [产品类别]"
            + ",[生产进度].[入库捆数]"
            + ",[生产进度].[入库重量]"
            + ",[生产进度].[填报人]"
            + " from [生产进度],[产品],[生产单位]"
            + " where [产品].[ID]=[生产进度].[产品类别ID] and [生产单位].[ID]=[生产进度].[分厂ID]";

        protected string selectid = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((object)Session["OperatorDID"] != null)
            {
                int did = (int)Session["OperatorDID"];
                if (did != 0)
                {
                    this.FenChang.Text = did.ToString();
                    this.FenChang.Enabled = false;
                    if (did == 1)
                    {
                        this.ChanPin.Text = "2";
                        this.ChanPin.Enabled = false;
                    }
                    else if (did == 5)
                    {
                        this.ChanPin.Text = "1";
                        this.ChanPin.Enabled = false;
                        this.GridView1.Columns[3].Visible = true;
                        this.Panel4.Visible = true;
                    }
                }
            }

            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            }

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("JinDu_In.aspx");
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            if (this.ChanPin.Text != "0")
            {
                sql += " and [生产进度].[产品类别ID]=" + this.ChanPin.Text.Replace("'", "");
            }
            if (this.FenChang.Text != "0")
            {
                sql += " and [生产进度].[分厂ID]=" + this.FenChang.Text.Replace("'", "");
            }
            if (this.GongDuan.Text != "0")
            {
                sql += " and [生产进度].[工段]=" + this.GongDuan.Text.Replace("'", "");
            }

            if (this.ShengChanXian.Text != "0")
            {
                sql += " and [生产进度].[生产线]=" + this.ShengChanXian.Text.Replace("'", "");
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and [生产进度].[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and [生产进度].[日期]<='" + Dt.ToShortDateString() + "'";
            }
            sql += " order by [日期] desc,[生产进度].[工段] asc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.GridView1.SelectedIndex = -1;
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "delete from [生产进度] where [id]= " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                Response.Redirect("JinDu_In.aspx?id=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectid = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongDuan.Text = "0";
            this.ChanPin.Text = "0";
            this.Date1.Text = "";
            this.Date2.Text = "";
            this.Button_Query_Click(null, null);
        }

    }
}
